VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "xlUnitUIManager"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit

Public Sub AddExcelAppMenu()

Dim newMenuItem As CommandBarControl
Dim ctl As CommandBarControl

    Set ctl = commandBarControlFromCaption(Application.CommandBars(1), XLUNITMENUCAPTION)

    If Not (ctl Is Nothing) Then
        Exit Sub
    End If

    Set newMenuItem = Application.CommandBars(1).Controls.Add(msoControlPopup, temporary:=True)
    newMenuItem.caption = XLUNITMENUCAPTION
    
    AddMenuItem newMenuItem, "New &Application", "CreateNewApplication"
    AddMenuItem newMenuItem, "New Test &Class", "AddNewTestClass"
    AddMenuItem newMenuItem, "&Options", "SetOptions"
    AddMenuItem newMenuItem, "A&bout", "ShowAbout"
    
End Sub

Public Sub AddToolbarButton(caption As String, actionName As String)

Dim cmd As CommandBar
Dim ctl As CommandBarControl

    Set cmd = commandBarFromName(XLUNIT_TOOLBAR_NAME)

    If cmd Is Nothing Then
        Set cmd = CommandBars.Add(XLUNIT_TOOLBAR_NAME)
    End If

    Set ctl = commandBarControlFromCaption(cmd, caption)

    If ctl Is Nothing Then
        Set ctl = cmd.Controls.Add(msoControlButton)
    End If

    With ctl
        .caption = caption
        .Visible = True
        .Enabled = True
        .Style = msoButtonCaption
        .OnAction = actionName
    End With

    cmd.Visible = True
    cmd.Position = msoBarTop
    cmd.Left = 900

End Sub

Public Sub TidyUp()

    DeleteXLUnitCommandBar XLUNIT_TOOLBAR_NAME
    DeleteXLUnitMenu
    
End Sub

Private Sub AddMenuItem(menuControl As CommandBarControl, theCaption As String, theAction As String)

    With menuControl.Controls.Add(Type:=msoControlButton, temporary:=True, ID:=1)
            
        .caption = theCaption
        .TooltipText = ""
        .Style = msoButtonCaption
        .OnAction = theAction

    End With

End Sub

Private Sub DeleteXLUnitMenu()

Dim cmd As CommandBar
Dim ctl As CommandBarControl

    Set cmd = Application.CommandBars(1)
    
    If cmd Is Nothing Then
        Exit Sub
    End If
    
    Set ctl = commandBarControlFromCaption(cmd, XLUNITMENUCAPTION)
    
    If ctl Is Nothing Then
        Exit Sub
    End If
    
    ctl.Delete

End Sub

Private Sub DeleteXLUnitCommandBar(cmdbarName As String)

Dim cmd As CommandBar

    Set cmd = commandBarFromName(cmdbarName)
    
    If cmd Is Nothing Then
        Exit Sub
    End If
    
    cmd.Delete

End Sub

Private Function commandBarFromName(searchName As String) As CommandBar

Dim cmd As CommandBar

    For Each cmd In CommandBars
        If cmd.Name = searchName Then
            Set commandBarFromName = cmd
            Exit For
        End If
    Next

End Function

Private Function commandBarControlFromCaption(cbar As CommandBar, caption As String) As CommandBarControl

Dim ctl As CommandBarControl

    For Each ctl In cbar.Controls
        If ctl.caption = caption Then
            Set commandBarControlFromCaption = ctl
            Exit Function
        End If
    Next

End Function

